from application.enumeration.ContentTypeEnum import ContentTypeEnum
from application.util.NoteUtil import note_model_to_note_out, static_url_to_id
from application.entity.NoteEntity import NoteAdd, NoteOut, NoteEdit
from application.enumeration.StatusCodeEnum import StatusCodeEnum
from application.exception.BasicException import BasicException
from application.mapper.CommentMapper import CommentMapper
from application.mapper.UserMapper import UserMapper
from application.mapper.NoteMapper import NoteMapper
from application.model.NoteModel import NoteModel
from typing import Optional


async def add_note(user_id: int, note_add: NoteAdd) -> None:
    """
    新增帖子
    :param user_id: 用户ID
    :param note_add: 帖子新增实体
    :return:
    """
    # 帖子新增实体赋值给ORM模型
    note_model: NoteModel = NoteModel(**note_add.model_dump())
    note_model.user_id = user_id
    # 静态URL转md5-index
    note_model = await static_url_to_id(note_input=note_add, note_model=note_model)
    # 插入帖子
    if not await NoteMapper.insert(data=note_model.to_dict()):
        raise BasicException(status_code=StatusCodeEnum.ERROR.value, message="新增帖子失败！")


async def delete_note_by_id(note_id: int, user_id: int) -> None:
    """
    根据帖子ID删除帖子
    :param note_id: 帖子ID
    :param user_id: 用户ID
    :return:
    """
    note_model: Optional[NoteModel] = await NoteMapper.get_data_by_field(id=note_id, user_id=user_id)
    if not note_model:
        raise BasicException(status_code=StatusCodeEnum.NOT_FOUND_ERROR.value, message="帖子不存在！")
    # 删除该帖子评论
    del_comment_status: bool = await CommentMapper.delete_by_field(content_type=ContentTypeEnum.NOTE, target_id=note_id)
    del_note_status: bool = await NoteMapper.delete(orm_model=note_model)  # 删除帖子
    # 判断删除都成功没
    if not all([del_comment_status, del_note_status]):
        raise BasicException(status_code=StatusCodeEnum.ERROR.value, message="删除帖子失败！")


async def get_note_by_id(note_id: int) -> NoteOut:
    """
    根据帖子ID获取帖子信息
    :param note_id: 帖子ID
    :return: 帖子详细信息
    """
    note_model: Optional[NoteModel] = await NoteMapper.get_data_by_id(data_id=note_id)
    if not note_model:
        raise BasicException(status_code=StatusCodeEnum.NOT_FOUND_ERROR.value, message="帖子不存在！")
    # 帖子信息转换为输出实体
    result: NoteOut = await note_model_to_note_out(note_model=note_model)
    return result


async def get_list_by_user_id(user_id: int, page: int, page_size: int) -> list[Optional[NoteOut]]:
    """
    根据用户ID查询帖子列表
    :param user_id: 用户ID
    :param page: 当前页
    :param page_size: 每页条数
    :return: 帖子信息列表
    """
    # 先检查用户是否存在
    if not await UserMapper.get_data_by_id(data_id=user_id):
        raise BasicException(status_code=StatusCodeEnum.NOT_FOUND_ERROR.value, message="该用户不存在！")
    # 查询帖子
    note_model_list: list[Optional[NoteModel]] = await NoteMapper.get_data_list(page=page, page_size=page_size,
                                                                                user_id=user_id)
    note_out_list: list = []
    # 帖子模型列表转换为输出实体列表
    for note_model in note_model_list:
        note_out_list.append(await note_model_to_note_out(note_model=note_model))
    return note_out_list


async def edite_note_by_id(note_edit: NoteEdit, user_id: int) -> None:
    """
    根据ID编辑帖子
    :param note_edit: 帖子编辑实体
    :param user_id: 当前用户ID
    :return:
    """
    note_model: Optional[NoteModel] = await NoteMapper.get_data_by_field(id=note_edit.id, user_id=user_id)
    if not note_model:
        raise BasicException(status_code=StatusCodeEnum.NOT_FOUND_ERROR.value, message="帖子不存在！")
    note_model.user_id = user_id
    # 静态URL转md5-index
    note_model = await static_url_to_id(note_input=note_edit, note_model=note_model)
    # 更新帖子
    if not await NoteMapper.update(data=note_model.to_dict()):
        raise BasicException(status_code=StatusCodeEnum.ERROR.value, message="编辑帖子失败！")
